from datetime import datetime
from typing import Union, List

from colorama import Fore, Back, Style

from ..grouped_logger import LogWriter
from ..abstract_logger import LOG_LEVELS, LOG_LEVEL_NAMES


LOG_LEVEL_COLORS = [
    "",
    Fore.WHITE+Style.DIM,
    Fore.WHITE+Style.BRIGHT,
    Fore.YELLOW,
    Fore.RED,
    Back.RED,
]


class StdLogWriter(LogWriter):
    level: int

    def __init__(self, level: Union[int, str]):
        if type(level) is str:
            level = LOG_LEVELS.get(level, 1)
        self.level = level

    def log(self, level: int, msg: str, date: datetime, path: List[str]):
        if level < self.level:
            return
        date_str = date.strftime('%m-%d %H:%M:%S.') + "%03d" % date.microsecond

        print(LOG_LEVEL_COLORS[level] + f"[{date_str}][{LOG_LEVEL_NAMES[level]}] {msg}" + Style.RESET_ALL)


